//
//  JCReadViewController.m
//  ABookOfCartoon
//
//  Created by dlios on 15-5-18.
//  Copyright (c) 2015年 Power Of Ants. All rights reserved.
//

#import "JCReadViewController.h"
#import "JCReadModel.h"
#import "JCCartoonViewController.h"

@interface JCReadViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tabelView;
@property(nonatomic,retain)NSMutableArray *dataArr;
@property(nonatomic,retain)NSString *str8;


@end

@implementation JCReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    self.tabBarController.tabBar.translucent = NO;
    [self getDataFromWeb];
}

-(void)creatTabelView
{
    self.tabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _tabelView.delegate = self;
    _tabelView.dataSource = self;
    [self.view addSubview:_tabelView];
    [_tabelView release];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    JCReadModel *m = _dataArr[indexPath.row];
    cell.textLabel.text = m.SectionName;
   // JCCartoonViewController *cartoon = [[JCCartoonViewController alloc]init];
   
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     JCReadModel *m = _dataArr[indexPath.row];
    JCCartoonViewController *cartoon = [[JCCartoonViewController alloc]init];
    self.str8 = [[[NSNumberFormatter alloc]init] stringFromNumber:m.SectionID];
    NSString *str = [NSString stringWithFormat:@"http://112.124.96.190:9090/manhuadaquan4appjobs/ComicHandle.ashx?method=picturelist&sectionid=%@", _str8];
    cartoon.str9 = str;
    NSLog(@"%@",cartoon.str9);
    [self.navigationController pushViewController:cartoon animated:YES];
}
-(void)getDataFromWeb
{
    NSString *str = [NSString stringWithFormat:@"http://112.124.96.190:9090/manhuadaquan4appjobs/ComicHandle.ashx?method=sectionlist&bookid=%@", _str7];
    NSLog(@"%@",str);
    NSString *strEncoding = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strEncoding];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSMutableArray *arr = result;
        self.dataArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in arr) {
            JCReadModel *m = [[JCReadModel alloc]initWithDic:dic];
            [_dataArr addObject:m];
            [m release];
        }
        [self creatTabelView];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
